package com.luoqiuy.poet.controller;

import com.luoqiuy.poet.entity.Author;
import com.luoqiuy.poet.entity.Constellation;
import com.luoqiuy.poet.entity.Contact;
import com.luoqiuy.poet.entity.Zodiac;
import com.luoqiuy.poet.service.iml.AuthorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@Controller
public class AuthorController {

    @Autowired
    private  AuthorServiceImpl authorService;


    @GetMapping("/")
    public String root() {
        return "index";
    }

    @RequestMapping("/index/regist")
    public String toRegist(){
        return "regist";
    }

    @RequestMapping("/author/contact")
    public String contribute(){
        return "detail/contact";
    }

    @PostMapping("/author/login")
    public String login(HttpServletRequest request, Author author, Model model){
        int status =  authorService.authorLogin(request,author);
        if(status == 0){
            return "index";
        }
        return "detail/bookDetail";
    }

    @GetMapping("/author/logoff")
    public String logoff(HttpServletRequest request) {
        authorService.logoff(request);
        return "index";
    }

    @PostMapping("/author/regist")
    public String regist( @RequestParam("regHeadImg")MultipartFile headImg, String regNumber, String regPassword,
                          String ensureRegPassword, String regEmail, String birDay, String gender, String phone,
                          Constellation constellation, Zodiac zodiac, String regName, HttpServletRequest request){
        int res =authorService.regist(regNumber, regPassword, ensureRegPassword, regEmail, birDay, gender, phone, headImg,
                constellation, zodiac, request, regName);
        if(res == 4 || res == 3 || res == 2 || res == 0){
            return "regist";
        }else {
            return "ensure";
        }
    }

    @PostMapping("/author/ersure")
    public String ensure(HttpServletRequest request, String ensureCode){
        int res = authorService.ensure(request, ensureCode);
        if (res == 0){
            return "ensure";
        }
        return "index";
    }

    @RequestMapping("/author/contact/put")
    public String contactPut(Contact contact){
        authorService.contactMe(contact);
        return "index";
    }

    @RequestMapping("/author/visit")
    public String visit(HttpServletRequest request,int goalId){
        authorService.visit(request, goalId);
        return "detail/visit";
    }
}
